package com.gyg.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.Environment;
import org.springframework.core.env.Profiles;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Contact;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

import java.util.ArrayList;

@Configuration  //加入到配置
@EnableSwagger2     //开启swagger2
public class SwaggerConfig {



    @Bean
    public Docket docket1(){
        return new Docket(DocumentationType.SWAGGER_2).groupName("A");
    }

    @Bean
    public Docket docket2(){
        return new Docket(DocumentationType.SWAGGER_2).groupName("B");
    }

    @Bean
    public Docket docket3(){
        return new Docket(DocumentationType.SWAGGER_2).groupName("C");
    }

    /**
     * 配置了swagger的docket的bean实例
     * http://localhost:8081/swagger-ui.html    访问swagger页面
     *
     * @return
     */
    @Bean
    public Docket docket(Environment environment) {

//        设置在不同的环境下打开或关闭swagger界面
//        设置要显示的swagger环境
        Profiles profiles = Profiles.of("dev", "test");
//        通过environment.acceptsProfiles判断是否处于自己设定的环境中
        boolean flag = environment.acceptsProfiles(profiles);

        System.out.println("当前环境：" + flag);

        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo())
                .groupName("灰小猿")
//                是否启动swagger，如果为false则swagger不能在浏览器中访问
                .enable(flag)
                .select()
//                RequestHandlerSelectors，配置要扫描接口的方式
//                basePackage指定要扫描的包
//                any表示全部都扫描
//                none表示都不扫描
                .apis(RequestHandlerSelectors.basePackage("com.gyg.controller"))
//                  过滤指定路径下的请求  ant("/gyg/**")
//                .paths(PathSelectors.ant("/gyg/**"))
                .build();
    }

    /**
     * 配置swagger的信息等于apiinfo
     *
     * @return
     */
    private ApiInfo apiInfo() {
        Contact DEFAULT_CONTACT = new Contact("灰小猿", "https://huixiaoyuan.blog.csdn.net/", "1330439415@qq.com");
        return new ApiInfo(
                "灰小猿的swaggerAPI文档",
                "我们的指尖有改变世界的力量！",
                "v1.0",
                "https://huixiaoyuan.blog.csdn.net",
                DEFAULT_CONTACT,
                "Apache 2.0",
                "http://www.apache.org/licenses/LICENSE-2.0",
                new ArrayList());

    }

}
